package com.hongxun.budget.utils;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.hongxun.budget.R;
import com.hongxun.budget.greendao.APCostEntityDao;
import com.hongxun.budget.greendao.APCriteriaEntityDao;
import com.hongxun.budget.greendao.APEstimateEntityDao;
import com.hongxun.budget.greendao.APOtherEntityDao;
import com.hongxun.budget.greendao.APRangeEntityDao;
import com.hongxun.budget.greendao.APWireEntityDao;
import com.hongxun.budget.greendao.DCrossCostEntityDao;
import com.hongxun.budget.greendao.DDeviceBuyEntityDao;
import com.hongxun.budget.greendao.DDeviceOldEntityDao;
import com.hongxun.budget.greendao.DDrawLineEntityDao;
import com.hongxun.budget.greendao.DMaterialBuyEntityDao;
import com.hongxun.budget.greendao.DMaterialCostEntityDao;
import com.hongxun.budget.greendao.DMaterialOldEntityDao;
import com.hongxun.budget.greendao.DOtherCostEntityDao;
import com.hongxun.budget.greendao.DPictureEntityDao;
import com.hongxun.budget.greendao.DPointEntityDao;
import com.hongxun.budget.greendao.DWorkContentEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.greendao.ECrossCostEntityDao;
import com.hongxun.budget.greendao.EDeviceBuyEntityDao;
import com.hongxun.budget.greendao.EDeviceOldEntityDao;
import com.hongxun.budget.greendao.EDrawLineEntityDao;
import com.hongxun.budget.greendao.EMaterialBuyEntityDao;
import com.hongxun.budget.greendao.EMaterialOldEntityDao;
import com.hongxun.budget.greendao.EPRangeEntityDao;
import com.hongxun.budget.greendao.EPWireEntityDao;
import com.hongxun.budget.greendao.EPointEntityDao;
import com.hongxun.budget.greendao.EProblemexistEntityDao;
import com.hongxun.budget.greendao.EWorkContentEntityDao;
import com.hongxun.budget.ui.dialog.ImgClickDialog;
import com.hongxun.entity.ImageUrl;
import com.hongxun.greenentity.SysUserEntity;
import com.hongxun.greenentity.tk.APBaseEntity;
import com.hongxun.greenentity.tk.APCostEntity;
import com.hongxun.greenentity.tk.APCriteriaEntity;
import com.hongxun.greenentity.tk.APEstimateEntity;
import com.hongxun.greenentity.tk.APOtherEntity;
import com.hongxun.greenentity.tk.APRangeEntity;
import com.hongxun.greenentity.tk.APWireEntity;
import com.hongxun.greenentity.tk.BBudgetItemEntity;
import com.hongxun.greenentity.tk.BBudgetStandardEntity;
import com.hongxun.greenentity.tk.BCrossQuantitiesEntity;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.tk.BDeviceEntity;
import com.hongxun.greenentity.tk.BDeviceQuantitiesEntity;
import com.hongxun.greenentity.tk.BEntryCriteriaEntity;
import com.hongxun.greenentity.tk.BEstimateEntity;
import com.hongxun.greenentity.tk.BMaterialEntity;
import com.hongxun.greenentity.tk.BMaterialQuantitiesEntity;
import com.hongxun.greenentity.tk.BOtherCostEntity;
import com.hongxun.greenentity.tk.BOtherQuantitiesEntity;
import com.hongxun.greenentity.tk.BPointConfigEntity;
import com.hongxun.greenentity.tk.BProjectApplicationEntity;
import com.hongxun.greenentity.tk.DCrossCostEntity;
import com.hongxun.greenentity.tk.DDeviceBuyEntity;
import com.hongxun.greenentity.tk.DDeviceOldEntity;
import com.hongxun.greenentity.tk.DDrawLineEntity;
import com.hongxun.greenentity.tk.DMaterialBuyEntity;
import com.hongxun.greenentity.tk.DMaterialCostEntity;
import com.hongxun.greenentity.tk.DMaterialOldEntity;
import com.hongxun.greenentity.tk.DOtherCostEntity;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.hongxun.greenentity.tk.DWorkContentEntity;
import com.hongxun.greenentity.ys.ECrossCostEntity;
import com.hongxun.greenentity.ys.EDeviceBuyEntity;
import com.hongxun.greenentity.ys.EDeviceOldEntity;
import com.hongxun.greenentity.ys.EDrawLineEntity;
import com.hongxun.greenentity.ys.EMaterialBuyEntity;
import com.hongxun.greenentity.ys.EMaterialOldEntity;
import com.hongxun.greenentity.ys.EPBaseEntity;
import com.hongxun.greenentity.ys.EPRangeEntity;
import com.hongxun.greenentity.ys.EPWireEntity;
import com.hongxun.greenentity.ys.EPointEntity;
import com.hongxun.greenentity.ys.EProblemEntity;
import com.hongxun.greenentity.ys.EProblemexistEntity;
import com.hongxun.greenentity.ys.EWorkContentEntity;
import com.hongxun.interfaces.NovaCallback;
import com.hongxun.interfaces.ResponseListener;
import com.previewlibrary.GPreviewBuilder;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.config.ISCameraConfig;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.ArrayList;
import java.util.List;

import dmax.dialog.SpotsDialog;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2018/5/16.
 */

public class XUtils {

    public static int CHOISE_IMG = 0x999;
    public static int CAMERA_IMG = 0x998;


    public static void imgClick(final Activity activity) {
        new ImgClickDialog().setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                int i = (int) data[0];
                if (i == 1) {
                    cameraImg(activity);
                } else if (i == 2) {
                    choiseImg(activity);
                }
            }
        }).show(activity.getFragmentManager(), "");
    }

    public static void loadImg(Context context, Object url, ImageView view) {
        if (view == null) return;
        Glide.with(context).load(url).error(R.drawable.ic_default_image).into(view);
    }

    public static void cameraImg(Object source) {
        ISCameraConfig config = new ISCameraConfig.Builder()
                .needCrop(false) // 裁剪
                .build();
        ISNav.getInstance().toCameraActivity(source, config, CAMERA_IMG);
    }

    /**
     * 图片选择器
     *
     * @param source
     */
    public static void choiseImg(Object source) {
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(true)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.TRANSPARENT)
                // “确定”按钮文字颜色
                .btnTextColor(Color.WHITE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.parseColor("#303F9F"))
                // 标题
                .title("图片选择")
                // 标题文字颜色
                .titleColor(Color.WHITE)
                // TitleBar背景色
                .titleBgColor(Color.parseColor("#3F51B5"))
                // 第一个是否显示相机，默认true
                .needCamera(false)
                // 最大选择图片数量，默认9
                .maxNum(9)
                .build();
        // 跳转到图片选择器
        ISNav.getInstance().toListActivity(source, config, CHOISE_IMG);
    }

    /**
     * 查看大图
     *
     * @param context
     * @param imageUrls
     * @param index
     */
    public static void showImg(Activity context, List<ImageUrl> imageUrls, int index) {
        GPreviewBuilder.from(context)
                .setData(imageUrls)
                .setCurrentIndex(index)
                .setSingleFling(false)//是否在黑屏区域点击返回
                .setDrag(false)//是否禁用图片拖拽返回
                .setType(GPreviewBuilder.IndicatorType.Number)//指示器类型
                .start();//启动
    }

    /**
     * 主线程实现去重查询
     *
     * @param columnName
     * @param tableName
     * @return
     */
    public static List<String> distinctQuery(String columnName, String tableName) {
        String sql = "SELECT DISTINCT " + columnName + " FROM " + tableName;
        return query(sql);
    }

    /**
     * 主线程实现去重查询
     *
     * @param columnName
     * @param tableName
     * @return
     */
    public static List<String> distinctQuery(String columnName, String tableName, String where) {
        String sql = "SELECT DISTINCT " + columnName + " FROM " + tableName + " where " + where;
        return query(sql);
    }

    /**
     * 子线程实现去重查询
     *
     * @param columnName
     * @param tableName
     * @param c
     */
    public static void distinctQuery(Context context, String columnName, String tableName, final NovaCallback c) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在获取数据...");
        spotsDialog.show();
        String sql = "SELECT DISTINCT " + columnName + " FROM " + tableName;
        Observable.just(sql).subscribeOn(Schedulers.io()).map(new Func1<String, List<String>>() {
            @Override
            public List<String> call(String s) {
                return query(s);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<String>>() {
            @Override
            public void call(List<String> list) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                JSONObject obj = new JSONObject();
                obj.put("list", list);
                c.success(9999, obj);
            }
        });
    }

    public static List<String> distinctQuery(Context context, String columnName, String tableName) {
        String sql = "SELECT DISTINCT " + columnName + " FROM " + tableName;
        return query(sql);
    }

    private static List<String> query(String sql) {
        Cursor cursor = GreenDaoManager.getInstance().getmDaoSession().getDatabase().rawQuery(sql, null);
        List<String> list = new ArrayList<>();
        if (cursor.moveToFirst()) {
            do {
                list.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
        return list;
    }

    /**
     * 本地缓存基础数据信息(基本信息)
     *
     * @param obj
     * @param c
     */
    public static void setBaseData(Context context, JSONObject obj, final NovaCallback c) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存基础数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                String bDeviceEntities = obj.getString("bDeviceEntities");//材料
                session.getBDeviceEntityDao().deleteAll();
                session.getBDeviceEntityDao().insertInTx(JSON.parseArray(bDeviceEntities, BDeviceEntity.class));
                String bBudgetStandardEntities = obj.getString("bBudgetStandardEntities");//标准
                session.getBBudgetStandardEntityDao().deleteAll();
                session.getBBudgetStandardEntityDao().insertInTx(JSON.parseArray(bBudgetStandardEntities, BBudgetStandardEntity.class));
                String bProjectApplicationEntities = obj.getString("bProjectApplicationEntities");//申请书
                session.getBProjectApplicationEntityDao().deleteAll();
                session.getBProjectApplicationEntityDao().insertInTx(JSON.parseArray(bProjectApplicationEntities, BProjectApplicationEntity.class));
//                String bDeviceQuantitiesEntities = obj.getString("bDeviceQuantitiesEntities");//设备工程量
//                session.getBDeviceQuantitiesEntityDao().deleteAll();
//                session.getBDeviceQuantitiesEntityDao().insertInTx(JSON.parseArray(bDeviceQuantitiesEntities, BDeviceQuantitiesEntity.class));
//                String bMaterialQuantitiesEntities = obj.getString("bMaterialQuantitiesEntities");//材料工程量
//                session.getBMaterialQuantitiesEntityDao().deleteAll();
//                session.getBMaterialQuantitiesEntityDao().insertInTx(JSON.parseArray(bMaterialQuantitiesEntities, BMaterialQuantitiesEntity.class));
                String bEstimateEntities = obj.getString("bEstimateEntities");//估算简表
                session.getBEstimateEntityDao().deleteAll();
                session.getBEstimateEntityDao().insertInTx(JSON.parseArray(bEstimateEntities, BEstimateEntity.class));
                String bPointConfigEntities = obj.getString("bPointConfigEntities");//点位配置表
                session.getBPointConfigEntityDao().deleteAll();
                session.getBPointConfigEntityDao().insertInTx(JSON.parseArray(bPointConfigEntities, BPointConfigEntity.class));
                String bMaterialEntities = obj.getString("bMaterialEntities");//材料
                session.getBMaterialEntityDao().deleteAll();
                session.getBMaterialEntityDao().insertInTx(JSON.parseArray(bMaterialEntities, BMaterialEntity.class));
                String bOtherCostEntities = obj.getString("bOtherCostEntities");//其他费用
                session.getBOtherCostEntityDao().deleteAll();
                session.getBOtherCostEntityDao().insertInTx(JSON.parseArray(bOtherCostEntities, BOtherCostEntity.class));
                String bBudgetItemEntities = obj.getString("bBudgetItemEntities");//预算项
                session.getBBudgetItemEntityDao().deleteAll();
                session.getBBudgetItemEntityDao().insertInTx(JSON.parseArray(bBudgetItemEntities, BBudgetItemEntity.class));
                String bOtherQuantitiesEntities = obj.getString("bOtherQuantitiesEntities");//其他工程量
                session.getBOtherQuantitiesEntityDao().deleteAll();
                session.getBOtherQuantitiesEntityDao().insertInTx(JSON.parseArray(bOtherQuantitiesEntities, BOtherQuantitiesEntity.class));
                String bEntryCriteriaEntities = obj.getString("bEntryCriteriaEntities");//准入条件
                session.getBEntryCriteriaEntityDao().deleteAll();
                session.getBEntryCriteriaEntityDao().insertInTx(JSON.parseArray(bEntryCriteriaEntities, BEntryCriteriaEntity.class));
                String bDataDictionaryEntities = obj.getString("bDataDictionaryEntities");//字典
                session.getBDataDictionaryEntityDao().deleteAll();
                session.getBDataDictionaryEntityDao().insertInTx(JSON.parseArray(bDataDictionaryEntities, BDataDictionaryEntity.class));
                String bCrossQuantitiesEntities = obj.getString("bCrossQuantitiesEntities");//交叉跨越预算项
                session.getBCrossQuantitiesEntityDao().deleteAll();
                session.getBCrossQuantitiesEntityDao().insertInTx(JSON.parseArray(bCrossQuantitiesEntities, BCrossQuantitiesEntity.class));
                String sysUserEntities = obj.getString("sysUserEntities");//人员
                session.getSysUserEntityDao().deleteAll();
                session.getSysUserEntityDao().insertInTx(JSON.parseArray(sysUserEntities, SysUserEntity.class));
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(3, null);
            }
        });
    }

    public static void setBaseData2(Context context, JSONObject obj, final NovaCallback c) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存设备工程量数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                String bDeviceQuantitiesEntities = obj.getString("bDeviceQuantitiesEntities");//设备工程量
                session.getBDeviceQuantitiesEntityDao().deleteAll();
                session.getBDeviceQuantitiesEntityDao().insertInTx(JSON.parseArray(bDeviceQuantitiesEntities, BDeviceQuantitiesEntity.class));
//                String bMaterialQuantitiesEntities = obj.getString("bMaterialQuantitiesEntities");//材料工程量
//                session.getBMaterialQuantitiesEntityDao().deleteAll();
//                session.getBMaterialQuantitiesEntityDao().insertInTx(JSON.parseArray(bMaterialQuantitiesEntities, BMaterialQuantitiesEntity.class));
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(4, null);
            }
        });
    }

    public static void setBaseData3(Context context, JSONObject obj, final NovaCallback c) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存材料工程量数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
//                String bDeviceQuantitiesEntities = obj.getString("bDeviceQuantitiesEntities");//设备工程量
//                session.getBDeviceQuantitiesEntityDao().deleteAll();
//                session.getBDeviceQuantitiesEntityDao().insertInTx(JSON.parseArray(bDeviceQuantitiesEntities, BDeviceQuantitiesEntity.class));
                String bMaterialQuantitiesEntities = obj.getString("bMaterialQuantitiesEntities");//材料工程量
                session.getBMaterialQuantitiesEntityDao().deleteAll();
                session.getBMaterialQuantitiesEntityDao().insertInTx(JSON.parseArray(bMaterialQuantitiesEntities, BMaterialQuantitiesEntity.class));
                String eProblemEntities = obj.getString("eProblemEntities");//隐患库
                session.getEProblemEntityDao().deleteAll();
                session.getEProblemEntityDao().insertInTx(JSON.parseArray(eProblemEntities, EProblemEntity.class));
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(5, null);
            }
        });
    }

    /**
     * 删除项目
     *
     * @return
     */
    public static void deleteProject(String guid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        session.getAPCostEntityDao().queryBuilder().where(APCostEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getAPCriteriaEntityDao().queryBuilder().where(APCriteriaEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getAPEstimateEntityDao().queryBuilder().where(APEstimateEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getAPOtherEntityDao().queryBuilder().where(APOtherEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getAPRangeEntityDao().queryBuilder().where(APRangeEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getAPWireEntityDao().queryBuilder().where(APWireEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDCrossCostEntityDao().queryBuilder().where(DCrossCostEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDDeviceBuyEntityDao().queryBuilder().where(DDeviceBuyEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDDeviceOldEntityDao().queryBuilder().where(DDeviceOldEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDDrawLineEntityDao().queryBuilder().where(DDrawLineEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDMaterialBuyEntityDao().queryBuilder().where(DMaterialBuyEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDMaterialCostEntityDao().queryBuilder().where(DMaterialCostEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDMaterialOldEntityDao().queryBuilder().where(DMaterialOldEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDOtherCostEntityDao().queryBuilder().where(DOtherCostEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDPictureEntityDao().queryBuilder().where(DPictureEntityDao.Properties.PGuid.eq(guid), DPictureEntityDao.Properties.PStatus.eq("储备")).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDPointEntityDao().queryBuilder().where(DPointEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDWorkContentEntityDao().queryBuilder().where(DWorkContentEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getAPBaseEntityDao().deleteByKey(guid);
    }

    /**
     * 删除项目
     *
     * @return
     */
    public static void deleteYsProject(String guid) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        session.getEPRangeEntityDao().queryBuilder().where(EPRangeEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEPWireEntityDao().queryBuilder().where(EPWireEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getECrossCostEntityDao().queryBuilder().where(ECrossCostEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEDrawLineEntityDao().queryBuilder().where(EDrawLineEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getDPictureEntityDao().queryBuilder().where(DPictureEntityDao.Properties.PGuid.eq(guid), DPictureEntityDao.Properties.PStatus.eq("验收")).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEPointEntityDao().queryBuilder().where(EPointEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEProblemexistEntityDao().queryBuilder().where(EProblemexistEntityDao.Properties.Projectguid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEDeviceBuyEntityDao().queryBuilder().where(EDeviceBuyEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEDeviceOldEntityDao().queryBuilder().where(EDeviceOldEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEMaterialBuyEntityDao().queryBuilder().where(EMaterialBuyEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEMaterialOldEntityDao().queryBuilder().where(EMaterialOldEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEWorkContentEntityDao().queryBuilder().where(EWorkContentEntityDao.Properties.PGuid.eq(guid)).buildDelete().executeDeleteWithoutDetachingEntities();
        session.getEPBaseEntityDao().deleteByKey(guid);
    }

    /**
     * 下载项目数据
     *
     * @param obj
     * @param c
     */
    public static void downProject(Context context, JSONObject obj, final NovaCallback c) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                String apBaseEntity = obj.getString("apBaseEntity");
                APBaseEntity base = JSON.parseObject(apBaseEntity, APBaseEntity.class);
                deleteProject(base.getGuid());//删除项目所有数据信息
                base.setPBDatastate("已同步");
                session.getAPBaseEntityDao().insertOrReplace(base);

                List<APCostEntity> apCostEntities = JSON.parseArray(obj.getString("apCostEntities"), APCostEntity.class);
                for (APCostEntity a : apCostEntities) {
                    a.setPCDatastate("已同步");
                }
                session.getAPCostEntityDao().insertOrReplaceInTx(apCostEntities);
                List<APCriteriaEntity> apCriteriaEntities = JSON.parseArray(obj.getString("apCriteriaEntities"), APCriteriaEntity.class);
                for (APCriteriaEntity a : apCriteriaEntities) {
                    a.setPCDatastate("已同步");
                }
                session.getAPCriteriaEntityDao().insertOrReplaceInTx(apCriteriaEntities);
                List<APEstimateEntity> apEstimateEntities = JSON.parseArray(obj.getString("apEstimateEntities"), APEstimateEntity.class);
                for (APEstimateEntity a : apEstimateEntities) {
                    a.setPEDatastate("已同步");
                }
                session.getAPEstimateEntityDao().insertOrReplaceInTx(apEstimateEntities);
                List<APOtherEntity> apOtherEntities = JSON.parseArray(obj.getString("apOtherEntities"), APOtherEntity.class);
                for (APOtherEntity a : apOtherEntities) {
                    a.setPODatastate("已同步");
                }
                session.getAPOtherEntityDao().insertOrReplaceInTx(apOtherEntities);
                List<APRangeEntity> apRangeEntities = JSON.parseArray(obj.getString("apRangeEntities"), APRangeEntity.class);
                for (APRangeEntity a : apRangeEntities) {
                    a.setPRDatastate("已同步");
                }
                session.getAPRangeEntityDao().insertOrReplaceInTx(apRangeEntities);
                List<APWireEntity> apWireEntities = JSON.parseArray(obj.getString("apWireEntities"), APWireEntity.class);
                for (APWireEntity a : apWireEntities) {
                    a.setPWDatastate("已同步");
                }
                session.getAPWireEntityDao().insertOrReplaceInTx(apWireEntities);
                List<DCrossCostEntity> dCrossCostEntities = JSON.parseArray(obj.getString("dCrossCostEntities"), DCrossCostEntity.class);
                for (DCrossCostEntity a : dCrossCostEntities) {
                    a.setCCDatastate("已同步");
                }
                session.getDCrossCostEntityDao().insertOrReplaceInTx(dCrossCostEntities);
                List<DDeviceBuyEntity> dDeviceBuyEntities = JSON.parseArray(obj.getString("dDeviceBuyEntities"), DDeviceBuyEntity.class);
                for (DDeviceBuyEntity a : dDeviceBuyEntities) {
                    a.setDBDatastate("已同步");
                }
                session.getDDeviceBuyEntityDao().insertOrReplaceInTx(dDeviceBuyEntities);
                List<DDeviceOldEntity> dDeviceOldEntities = JSON.parseArray(obj.getString("dDeviceOldEntities"), DDeviceOldEntity.class);
                for (DDeviceOldEntity a : dDeviceOldEntities) {
                    a.setDRDatastate("已同步");
                }
                session.getDDeviceOldEntityDao().insertOrReplaceInTx(dDeviceOldEntities);
                List<DDrawLineEntity> dDrawLineEntities = JSON.parseArray(obj.getString("dDrawLineEntities"), DDrawLineEntity.class);
                for (DDrawLineEntity a : dDrawLineEntities) {
                    a.setDLDatastate("已同步");
                }
                session.getDDrawLineEntityDao().insertOrReplaceInTx(dDrawLineEntities);
                List<DMaterialBuyEntity> dMaterialBuyEntities = JSON.parseArray(obj.getString("dMaterialBuyEntities"), DMaterialBuyEntity.class);
                for (DMaterialBuyEntity a : dMaterialBuyEntities) {
                    a.setMBDatastate("已同步");
                }
                session.getDMaterialBuyEntityDao().insertOrReplaceInTx(dMaterialBuyEntities);
                List<DMaterialCostEntity> dMaterialCostEntities = JSON.parseArray(obj.getString("dMaterialCostEntities"), DMaterialCostEntity.class);
                for (DMaterialCostEntity a : dMaterialCostEntities) {
                    a.setMCDatastate("已同步");
                }
                session.getDMaterialCostEntityDao().insertOrReplaceInTx(dMaterialCostEntities);
                List<DMaterialOldEntity> dMaterialOldEntities = JSON.parseArray(obj.getString("dMaterialOldEntities"), DMaterialOldEntity.class);
                for (DMaterialOldEntity a : dMaterialOldEntities) {
                    a.setMBDatastate("已同步");
                }
                session.getDMaterialOldEntityDao().insertOrReplaceInTx(dMaterialOldEntities);
                List<DOtherCostEntity> dOtherCostEntities = JSON.parseArray(obj.getString("dOtherCostEntities"), DOtherCostEntity.class);
                for (DOtherCostEntity a : dOtherCostEntities) {
                    a.setCCDatastate("已同步");
                }
                session.getDOtherCostEntityDao().insertOrReplaceInTx(dOtherCostEntities);
                List<DPictureEntity> dPictureEntities = JSON.parseArray(obj.getString("dPictureEntities"), DPictureEntity.class);
                for (DPictureEntity a : dPictureEntities) {
                    a.setPDatastate("已同步");
                }
                session.getDPictureEntityDao().insertOrReplaceInTx(dPictureEntities);
                List<DPointEntity> dPointEntities = JSON.parseArray(obj.getString("dPointEntities"), DPointEntity.class);
                for (DPointEntity a : dPointEntities) {
                    a.setPPDatastate("已同步");
                }
                session.getDPointEntityDao().insertOrReplaceInTx(dPointEntities);
                List<DWorkContentEntity> dWorkContentEntities = JSON.parseArray(obj.getString("dWorkContentEntities"), DWorkContentEntity.class);
                for (DWorkContentEntity a : dWorkContentEntities) {
                    a.setWCDatastate("已同步");
                }
                session.getDWorkContentEntityDao().insertOrReplaceInTx(dWorkContentEntities);
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(6, null);
            }
        });
    }

    /**
     * 下载隐患数据
     *
     * @param obj
     * @param c
     */
    public static void downProblem(Context context, JSONObject obj, final NovaCallback c) {
        final SpotsDialog spotsDialog = new SpotsDialog(context, "正在缓存数据...");
        spotsDialog.show();
        Observable.just(obj).subscribeOn(Schedulers.io()).map(new Func1<JSONObject, Boolean>() {
            @Override
            public Boolean call(JSONObject obj) {
                DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
                String epBaseEntity = obj.getString("epBaseEntity");
                EPBaseEntity base = JSON.parseObject(epBaseEntity, EPBaseEntity.class);
                deleteYsProject(base.getGuid());
                base.setPBDatastate("已同步");
                session.getEPBaseEntityDao().insertOrReplace(base);

                List<ECrossCostEntity> eCrossCostEntities = JSON.parseArray(obj.getString("eCrossCostEntities"), ECrossCostEntity.class);
                for (ECrossCostEntity a : eCrossCostEntities) {
                    a.setCCDatastate("已同步");
                }
                session.getECrossCostEntityDao().insertOrReplaceInTx(eCrossCostEntities);
                List<EDrawLineEntity> eDrawLineEntities = JSON.parseArray(obj.getString("eDrawLineEntities"), EDrawLineEntity.class);
                for (EDrawLineEntity a : eDrawLineEntities) {
                    a.setDLDatastate("已同步");
                }
                session.getEDrawLineEntityDao().insertOrReplaceInTx(eDrawLineEntities);
                List<EPointEntity> ePointEntities = JSON.parseArray(obj.getString("ePointEntities"), EPointEntity.class);
                for (EPointEntity a : ePointEntities) {
                    a.setPPDatastate("已同步");
                }
                session.getEPointEntityDao().insertOrReplaceInTx(ePointEntities);
                List<EPRangeEntity> epRangeEntities = JSON.parseArray(obj.getString("epRangeEntities"), EPRangeEntity.class);
                for (EPRangeEntity a : epRangeEntities) {
                    a.setPRDatastate("已同步");
                }
                session.getEPRangeEntityDao().insertOrReplaceInTx(epRangeEntities);
                List<EPWireEntity> epWireEntities = JSON.parseArray(obj.getString("epWireEntities"), EPWireEntity.class);
                for (EPWireEntity a : epWireEntities) {
                    a.setPWDatastate("已同步");
                }
                session.getEPWireEntityDao().insertOrReplaceInTx(epWireEntities);
                List<EProblemexistEntity> eProblemexistEntities = JSON.parseArray(obj.getString("eProblemexistEntities"), EProblemexistEntity.class);
                for (EProblemexistEntity a : eProblemexistEntities) {
                    a.setDatastate("已同步");
                }
                session.getEProblemexistEntityDao().insertOrReplaceInTx(eProblemexistEntities);
                List<DPictureEntity> dPictureEntities = JSON.parseArray(obj.getString("dPictureEntities"), DPictureEntity.class);
                for (DPictureEntity a : dPictureEntities) {
                    a.setPDatastate("已同步");
                }
                session.getDPictureEntityDao().insertOrReplaceInTx(dPictureEntities);
                List<EDeviceOldEntity> eDeviceOldEntities = JSON.parseArray(obj.getString("eDeviceOldEntities"), EDeviceOldEntity.class);
                for (EDeviceOldEntity a : eDeviceOldEntities) {
                    a.setDRDatastate("已同步");
                }
                session.getEDeviceOldEntityDao().insertOrReplaceInTx(eDeviceOldEntities);
                List<EDeviceBuyEntity> eDeviceBuyEntities = JSON.parseArray(obj.getString("eDeviceBuyEntities"), EDeviceBuyEntity.class);
                for (EDeviceBuyEntity a : eDeviceBuyEntities) {
                    a.setDBDatastate("已同步");
                }
                session.getEDeviceBuyEntityDao().insertOrReplaceInTx(eDeviceBuyEntities);
                List<EMaterialBuyEntity> eMaterialBuyEntities = JSON.parseArray(obj.getString("eMaterialBuyEntities"), EMaterialBuyEntity.class);
                for (EMaterialBuyEntity a : eMaterialBuyEntities) {
                    a.setMBDatastate("已同步");
                }
                session.getEMaterialBuyEntityDao().insertOrReplaceInTx(eMaterialBuyEntities);
                List<EMaterialOldEntity> eMaterialOldEntities = JSON.parseArray(obj.getString("eMaterialOldEntities"), EMaterialOldEntity.class);
                for (EMaterialOldEntity a : eMaterialOldEntities) {
                    a.setMBDatastate("已同步");
                }
                session.getEMaterialOldEntityDao().insertOrReplaceInTx(eMaterialOldEntities);
                List<EWorkContentEntity> eWorkContentEntities = JSON.parseArray(obj.getString("eWorkContentEntities"), EWorkContentEntity.class);
                for (EWorkContentEntity a : eWorkContentEntities) {
                    a.setWCDatastate("已同步");
                }
                session.getEWorkContentEntityDao().insertOrReplaceInTx(eWorkContentEntities);
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                spotsDialog.dismiss();
                spotsDialog.cancel();
                c.success(6, null);
            }
        });
    }

}
